﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace M2B.GestionEntretiens.Controllers
{
    public class EntretiensController : Controller
    {
        //
        // GET: /Entretiens/

        public ActionResult Index()
        {
            return View();
        }


        public class JsonEvent
        {
            public string id { get; set; }
            public string text { get; set; }
            public string start { get; set; }
            public string end { get; set; }

            public string name { get; set; }
        }

        public ActionResult Events(DateTime? start, DateTime? end)
        {

            // SQL: SELECT * FROM [event] WHERE NOT (([end] <= @start) OR ([start] >= @end))
            var events = new List<JsonEvent>();

            var result = events
            .Select(e => new JsonEvent()
            {
                start = e.start.ToString(),
                end = e.end.ToString(),
                text = e.name,
                id = e.id.ToString()
            })
            .ToList();

            return new JsonResult { Data = result };
        }

        public ActionResult Create(string start, string end, string name)
        {
            var toBeCreated = new JsonEvent
            {
                start = Convert.ToDateTime(start).ToString(),
                end = Convert.ToDateTime(end).ToString(),
                name = name
            };
            //db.Events.InsertOnSubmit(toBeCreated);
            //db.SubmitChanges();

            return new JsonResult { Data = new Dictionary<string, object> { { "id", toBeCreated.id } } };

        }

        public ActionResult Move(int id, string newStart, string newEnd)
        {
            //var toBeResized = (from ev in db.Events where ev.id == id select ev).First();
            var toBeResized = new JsonEvent();
            toBeResized.start = Convert.ToDateTime(newStart).ToString();
            toBeResized.end = Convert.ToDateTime(newEnd).ToString();
           // db.SubmitChanges();

            return new JsonResult { Data = new Dictionary<string, object> { { "id", toBeResized.id } } };
        }

        public ActionResult Resize(int id, string newStart, string newEnd)
        {
            //var toBeResized = (from ev in db.Events where ev.id == id select ev).First();
            var toBeResized = new JsonEvent();
            toBeResized.start = Convert.ToDateTime(newStart).ToString();
            toBeResized.end = Convert.ToDateTime(newEnd).ToString();
            //db.SubmitChanges();

            return new JsonResult { Data = new Dictionary<string, object> { { "id", toBeResized.id } } };
        }

    }
}
